package com.huluobo.a2206douyindemo.startLive;

import static com.tencent.live2.V2TXLiveCode.V2TXLIVE_ERROR_INVALID_LICENSE;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.huluobo.a2206douyindemo.R;
import com.huluobo.a2206douyindemo.databinding.ActivityStartLiveBinding;
import com.huluobo.base.mvvm.BaseActivity;
import com.tencent.live2.V2TXLiveDef;
import com.tencent.live2.V2TXLivePusher;
import com.tencent.live2.impl.V2TXLivePusherImpl;
import com.tencent.rtmp.ui.TXCloudVideoView;

public class StartLiveActivity extends BaseActivity<ActivityStartLiveBinding> {

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        // 指定对应的直播协议为 RTMP，该协议不支持连麦
        V2TXLivePusher mLivePusher = new V2TXLivePusherImpl(this, V2TXLiveDef.V2TXLiveMode.TXLiveMode_RTMP);
        // 初始化时启动本地摄像头预览
        TXCloudVideoView mPusherView = (TXCloudVideoView) findViewById(R.id.pusher_tx_cloud_view);
        mLivePusher.setRenderView(mPusherView);
        mLivePusher.startCamera(true);
        mLivePusher.startMicrophone();
        //点击按钮开播
        binding.startLiveBtn.setOnClickListener(v -> {
            // 根据推流协议传入对应的 URL 即可启动推流， RTMP 协议以 rtmp:// 开头，该协议不支持连麦
            String url = "rtmp://204071.push.tlivecloud.com/live/123?txSecret=dc1ccaadc58bfe819cb5df5de3c4d556&txTime=66D285EB";
            // 根据推流协议传入对应的 URL 即可启动推流， RTC 协议以 trtc:// 开头，该协议支持连麦
            int ret = mLivePusher.startPush(url);
            if (ret == V2TXLIVE_ERROR_INVALID_LICENSE) {
                Log.i("TagA", "startRTMPPush: license 校验失败");
            }
        });

        binding.startLiveStopBtn.setOnClickListener(v -> {
            // 结束推流
            mLivePusher.stopPush();
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_start_live;
    }
}